<template>
  <div class="dashboard-container">
    <div>
      <div>
        <el-row>
          <span class="label_style" style="float: left;margin:8px 0 0 0">课程分类：</span>
          <el-col :span="7">
            <div class="grid-content bg-purple">
              <div class="grid-content bg-purple">
                <el-select @change="queryTwoLevel(ruleForm.course_parentid)" v-model="ruleForm.course_parentid" filterable
                           placeholder="一级分类">
                  <el-option v-for="item in oneLevel" :key="item.id" :label="item.cat_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </div>
          </el-col>
          <el-col :span="7">
            <div class="grid-content bg-purple-light">
              <div class="grid-content bg-purple-light">
                <el-select @change="queryThreeLevel(ruleForm.course_secondid)" v-model="ruleForm.course_secondid"
                           filterable placeholder="二级分类">
                  <el-option v-for="item in twoLevel" :key="item.id" :label="item.cat_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </div>
          </el-col>
          <el-col :span="7">
            <div class="grid-content bg-purple">
              <div class="grid-content bg-purple">
                <el-select v-model="ruleForm.course_catid" filterable placeholder="三级分类">
                  <el-option v-for="item in threeLevel" :key="item.id" :label="item.cat_name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <div style="margin-top: 20px">
              <span class="label_style">请选择状态：</span>
              <el-select v-model="ruleForm.course_status" placeholder="请选择">
                <el-option v-for="item in status" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <div style="display: flex; width: 620px;margin-top: 20px">
                <span style="display: flex; width: 105px; align-items: center; justify-content: center;">课程名称：</span>
                <el-input v-model="ruleForm.course_title" placeholder="请输入课程名称" style="width: 50%"></el-input>
              </div>
            </div>
          </el-col>
        </el-row>

      </div>
      <div style="margin-top: 15px;display: flex;">
        <div class="el-select" style="width: 500px;">
          <el-button type="primary" class="div-button" @click="search">搜索</el-button>
          <el-button type="primary" class="div-button" @click="reset">重置</el-button>
        </div>
      </div>
      <el-row style="margin-top: 15px;">
        <el-button type="primary" @click="jumpToAdd"
                   style="padding: 12px 40px;">添加
        </el-button>
      </el-row>
    </div>
    <div>
      <el-main style="margin-top: 15px;padding: 0;">
        <el-table ref="menuTable"
                  style="width: 100%"
                  :data="list"
                  border>
          <el-table-column label="id" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="课程名称" align="center">
            <template slot-scope="scope">{{ scope.row.course_title }}</template>
          </el-table-column>
          <el-table-column label="学校名称" align="center">
            <template slot-scope="scope">{{ scope.row.school_name }}</template>
          </el-table-column>
          <el-table-column label="课程分类" align="center">
            <template slot-scope="scope">{{ scope.row.cat_name }}</template>
          </el-table-column>
          <el-table-column label="排序" align="center">
            <!--<template slot-scope="scope">{{ scope.row.cat_listorder }}</template>-->
            <template slot-scope="scope">
              <el-form :model="scope.row">
                <el-form-item size="mini">
                  <el-input v-enter-number v-model="scope.row.course_orderid"
                            @blur="changeNum(scope.$index, scope.row)"></el-input>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="是否开启" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="handleHiddenChange(scope.$index, scope.row)"
                :active-value="1"
                :inactive-value="0"
                v-model="scope.row.course_status">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.$index, scope.row)">编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </div>
    <div class="block block-div">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage4"
                     :page-sizes="[10, 20, 30, 40, 50]" :page-size="10" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import {cateAjax, cateComList, courseList, schooList, courseAjax, courseDel, cityDel} from "@/api/menu";
import {mapGetters} from "vuex";

export default {
  data() {
    return {
      status: [{
        value: '0',
        label: '未完成'
      }, {
        value: '1',
        label: '已完成'
      }],
      ruleForm: {
        course_title: '',
        course_parentid: '',
        course_secondid: '',
        course_catid: '',
        course_status: '',
        page: 1,
        page_size: 10
      },
      oneLevel: [],
      twoLevel: [],
      threeLevel: [],
      list: [],
      total: null,
      value: '',
      input: '',
      currentPage4: 1,
    }
  },
  computed: {
    ...mapGetters([
      'schoolId'
    ]),
  },
  mounted() {
    this.getOneLevel()
    this.getClassList()
  },
  methods: {
    /**
     * 跳转新增页面
     */
    jumpToAdd(){
      this.$router.push({path: '/classAdd', query: {id: this.$route.query.id}})
    },
    /**
     * 是否在前台显示
     * @param index
     * @param row
     */
    handleHiddenChange(index, row) {
      courseAjax({id: row.id, field: "course_status", value: row.course_status}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 获取列表单条信息
     * @param index
     * @param row
     */
    handleUpdate(index, row) {
      this.$router.push({path: '/classUpdate', query: {rowId: row.id, id: this.$route.query.id}});

      console.log(row)
    },
    /**
     * 排序
     * @param index
     * @param row
     */
    changeNum(index, row) {
      courseAjax({id: row.id, field: "course_orderid", value: row.course_orderid}).then(response => {
        this.$message({
          message: '修改成功',
          type: 'success',
          duration: 1000
        });
      });
    },
    /**
     * 重置
     */
    reset() {
      this.ruleForm = {
        course_title: '',
        course_parentid: '',
        course_secondid: '',
        course_catid: '',
        course_status: '',
        page: 1,
        page_size: 10
      };
      this.getClassList();
    },
    /**
     * 搜索
     */
    search() {
      this.getClassList()
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    /**
     * 删除
     * @param index
     * @param row
     */
    handleDelete(index, row) {
      this.$confirm('是否要删除该菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        courseDel(row.id).then(response => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1000
          });
          this.getClassList();
        });
      }).catch(e => e);
    },
    handleSizeChange(val) {
      this.ruleForm.page_size = val;
      this.getClassList();
    },
    handleCurrentChange(val) {
      this.ruleForm.page = val;
      this.getClassList();
    },
    /**
     * 获取一级数据
     */
    getOneLevel() {
      cateComList({
        cate_parentid: 0,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.oneLevel = response.data
        this.oneLevel.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 查询二级数据
     */
    queryTwoLevel(value) {
      console.log(value);
      this.ruleForm.course_secondid = '';
      this.ruleForm.course_catid = '';
      cateComList({
        cate_parentid: value,
        cate_secondid: 0
      }).then(response => {
        console.log(response);
        this.twoLevel = response.data
        this.twoLevel.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 查询三级数据
     */
    queryThreeLevel(value) {
      this.ruleForm.course_catid = '';
      console.log(value);
      cateComList({
        cate_parentid: value,
        cate_secondid: this.ruleForm.course_parentid
      }).then(response => {
        console.log(response);
        this.threeLevel = response.data
        this.threeLevel.unshift({id: "", cat_name: "请选择"})
      })
    },
    /**
     * 列表
     */
    getClassList() {
     // courseList(Object.assign(this.ruleForm, {course_schoolid: this.schoolId ? this.schoolId : this.$route.query.id})).then(response => {
      courseList(Object.assign(this.ruleForm, {course_schoolid: this.schoolId})).then(response => {
        console.log(response);
        this.list = response.data.data_list;
        this.total = response.data.total;
      })
    },
  }
}
</script>

<style lang="scss" scoped>
.block-div {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 20px;
}

.el-select:nth-child(2) {
  margin: 0px 10px;
}

.div-button {
  padding: 12px 0;
  width: 30%;
  margin-left: 0px;
}

.div-button:nth-child(2) {
  margin-left: 20px;
}

.dashboard {
  &-container {
    margin: 0 15px;
    padding: 30px;
    background-color: #fff;
  }

  &-text {
    font-size: 16px;
  }
}
</style>
